<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use App\User;


class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //展示用户列表
        /*$users = DB::table('users')->paginate(3);
        return view('admin.user_index')->with('users',$users);*/

        $count = DB::table('users')->count('id');
        $search = empty(trim($request->search)) ? '' : trim($request->search);

        //拼接sql语句
        $users = DB::table('users')->where('name', 'like', '%'.$search.'%')->paginate(3);
        return view('admin.user_index')->with('users',$users)->with('search',$search)->with('count',$count);

//        return view('admin.user_index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //加载用户添加页面
        return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //错误信息提示
        $messages = [
            'same'    => ':attribute 和 :other 必须相同。',
            'required'    => ':attribute :必须填。',
            'between' => ':attribute 必须介于 :min - :max。',
        ];
        //后台的数据验证
        $this->validate($request, [
            'name' => 'required|max:100',
            'email' => 'required|unique:users',
            'mobile' => 'required|unique:users|max:255',
            'password' => 'required',
        ],$messages);

        //获取数据'password' => bcrypt($data['password']),
        $user['name']=$request->name;
        $user['email']=$request->email;
        $user['mobile']=$request->mobile;
        $user['password']=bcrypt($request->password);
        $user['created_at']=$request->created_at;
        $user['updated_at']=$request->updated_at;
        //执行添加
        $users = DB::table('users')->insert($user);
        return redirect('admin/user');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $users = DB::table('users')->find($id);
        dd($users);
        //用户个人详情
        return view('admin.user_show')->with('users',$users);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //编辑页面
        $users = DB::table('users')->where('id',$id)->first();
        return view('admin.user_edit')->with('users',$users);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //执行修改
        $users = User::find($id);
        $users['name']=$request->name;
        $users['email']=$request->email;
        $users['mobile']=$request->mobile;
        $users['updated_at']=$request->updated_at;
        $users->save();
        /*$users = User::where('id',$id)->update(['name'=>$request->name,'mobile'=>$request->mobile,
         		'email'=>$request->email]);*/

        //执行更改
        return redirect('admin/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('users')->where('id',$id)->delete();
        if($res){
            return view('/admin/user');
        }else{
            return back();
        }
    }

    public function del($id)
    {
        $res = DB::table('users')->where('id',$id)->delete();
        if($res){
            return 1;
        }else{
            return 0;
        }
    }



}
